﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using MODEL;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.HSSF.Util;
namespace winUi
{
    public partial class FormHistoryStock : Form
    {
        public FormHistoryStock()
        {
            InitializeComponent();
        }
        DataTable hisdata = new DataTable();
        private void FormHistoryStock_Load(object sender, EventArgs e)
        {
            dgvStockDetail.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dgvStockDetail.MultiSelect = false;
            dgvStockDetail.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            List<historystock> hstocks = new BllHistoryStock().GetAllList();
            foreach (historystock hs in hstocks)
            {
                
                TreeNode node = new TreeNode();
                node.Tag = hs.Id;
                node.Text = hs.Date.Year.ToString()+"年"+hs.Date.Month.ToString()+"月";
                tvDate.Nodes.Add(node);
            }
        }

        private void tvDate_Click(object sender, EventArgs e)
        {
            
        }

        private void tvDate_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
        }

        private void tvDate_AfterCheck(object sender, TreeViewEventArgs e)
        {
            //MessageBox.Show(tvDate.SelectedNode.Text);
        }

        private void tvDate_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            tvDate.SelectedNode = e.Node;
            //MessageBox.Show(e.Node.Tag.ToString());
            hisdata = new BllHistoryStockDetail().GetTablesByHisId(Convert.ToInt32(e.Node.Tag));
            dgvStockDetail.DataSource = hisdata;
        }

        private void button2_Click(object sender, EventArgs e)
        {
             
            FolderBrowserDialog sfd = new FolderBrowserDialog();
            sfd.RootFolder = Environment.SpecialFolder.CommonDesktopDirectory;
            DialogResult result = sfd.ShowDialog();
            string fileName = "历史库存" + tvDate.SelectedNode.Text;
            if (result == DialogResult.OK)
            {               
                    string filePath = Path.Combine(sfd.SelectedPath, fileName+".xls");
                    if (File.Exists(filePath))
                    {
                        fileName = fileName + DateTime.Now.Millisecond.ToString();
                    }
                    fileName = Path.Combine(sfd.SelectedPath, fileName+".xls");
                    //fileName = sfd.SelectedPath+ + fileName;
                    FileStream fs = new FileStream(fileName,FileMode.OpenOrCreate);
                    HSSFWorkbook wb = new HSSFWorkbook();
                    ISheet sheet = wb.CreateSheet(tvDate.SelectedNode.Text);
                    //创建表头
                    //第一行
                    //创建字体样式
                    IFont font1 = wb.CreateFont();
                    font1.Color = HSSFColor.BLACK.index;
                    font1.IsItalic = true;
                    font1.Boldweight = 20;
                    font1.Underline = (byte)FontUnderlineType.NONE;
                    font1.FontHeightInPoints = 20;
                    ICellStyle style1 = wb.CreateCellStyle();
                    style1.SetFont(font1);
                    IRow titleRow = sheet.CreateRow(0);
                    ICell cell1 = titleRow.CreateCell(0);
                    cell1.CellStyle = style1;
                    cell1.SetCellType(CellType.STRING);
                    cell1.SetCellValue(DateTime.Now.ToShortDateString());
                    titleRow.CreateCell(1).SetCellValue("物料库存状态");
                    
                    //第二行
                    IRow headerRow = sheet.CreateRow(1);
                    headerRow.CreateCell(0).SetCellValue("物料名称");
                    headerRow.CreateCell(1).SetCellValue("型号");
                    headerRow.CreateCell(2).SetCellValue("产品规格");
                    headerRow.CreateCell(3).SetCellValue("月初库存");
                    headerRow.CreateCell(4).SetCellValue("累计入库");
                    headerRow.CreateCell(5).SetCellValue("累计退库");
                    headerRow.CreateCell(6).SetCellValue("累计出库");
                    headerRow.CreateCell(7).SetCellValue("即时库存");
                    headerRow.CreateCell(8).SetCellValue("单位");
                    headerRow.CreateCell(9).SetCellValue("单价");

                    string className = string.Empty;
                    int b = 0;
                    for (int i = 2 ; i < hisdata.Rows.Count+2+b; i++)
                    {
                        DataRow datarow = hisdata.Rows[i-2-b];
                        IRow row = sheet.CreateRow(i);
                        if (!className.Equals(datarow[1]))
                        {
                            className = datarow[1].ToString();
                            ICell celltype = row.CreateCell(0);
                            celltype.SetCellValue(className);
                            celltype.CellStyle = style1;
                            b++;
                            continue;
                        }
                        row.CreateCell(0).SetCellValue(datarow[2].ToString());//物料名称
                        row.CreateCell(1).SetCellValue(datarow[3].ToString());//型号
                        row.CreateCell(2).SetCellValue(datarow[4].ToString());//产品规格
                        row.CreateCell(3).SetCellValue((int)datarow[7]);//月初库存
                        row.CreateCell(4).SetCellValue((int)datarow[8]);//累计入库
                        row.CreateCell(5).SetCellValue((int)datarow[10]);//累计退库
                        row.CreateCell(6).SetCellValue((int)datarow[9]);//累计出库
                        row.CreateCell(7).SetCellValue((int)datarow[11]);//即时库存
                        row.CreateCell(8).SetCellValue(datarow[5].ToString());//单位
                        row.CreateCell(9).SetCellValue(Convert.ToDouble(datarow[15]));//单价

                    }
                    wb.Write(fs);
                    fs.Close();
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //new ServiceRuKu().GenerateStartHisStock(DateTime.Parse(tvDate.SelectedNode.Text));
            new ServiceRuKu().GenerateStartHisStock(DateTime.Parse(tvDate.SelectedNode.Text));
            MessageBox.Show("更新完成");
            hisdata = new BllHistoryStockDetail().GetTablesByHisId(Convert.ToInt32(tvDate.SelectedNode.Tag));
            dgvStockDetail.DataSource = hisdata;
        }
    }
}
